﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicareModel;
using MySql.Data.MySqlClient;

namespace MedicareDataAccess
{
    public class EventDAO : DAOConnection
    {
        public static EventDAO GetInstance()
        {
            return new EventDAO();
        }

        public Event getEventByID(int idEvent)
        {
            MySqlCommand cm = DBGetCommand(string.Format("SELECT * FROM events WHERE idEvent={0}", idEvent));
            MySqlDataReader dr = cm.ExecuteReader();
            if (!dr.Read())
            {
                cm.Connection.Close();
                return null;
            }

            Event anEvent = new Event();
            anEvent.idEvent = (int)dr["idEvent"];

            PatientDAO patients = new PatientDAO(); 
            anEvent.patient = patients.getPatientByID((int)dr["idPatient"]);

            TriageDAO triage = new TriageDAO();
            anEvent.triage = triage.getTriageByID((int)dr["idTriage"]);

//          EMTDAO emts = new EMTDAO();
//          anEvent.emt = emts.getEMTByID((int)dr["idEMT"]);

            anEvent.status = (string)dr["status"];
            anEvent.comments = (string)dr["comments"];


            return anEvent;
        }

        public List<Event> getAllEvents()
        {
            List<Event> events = new List<Event>(); 
            MySqlCommand cm = DBGetCommand(string.Format("SELECT * FROM events"));
            MySqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                Event anEvent = new Event();
                anEvent.idEvent = (int)dr["idEvent"];

                PatientDAO patients = new PatientDAO();
                anEvent.patient = patients.getPatientByID((int)dr["idPatient"]);

                TriageDAO triage = new TriageDAO();
                anEvent.triage = triage.getTriageByID((int)dr["idTriage"]);

                anEvent.status = (string)dr["status"];
                anEvent.comments = (string)dr["comments"];

                events.Add(anEvent);
            }

            cm.Connection.Close();
            return events; 
        }

        public List<EventInfo> getPendingEventsInfo()
        {
            List<EventInfo> events = new List<EventInfo>(); 
            MySqlCommand cm = DBGetCommand(string.Format("SELECT * FROM events ORDER BY idTriage DESC"));
            MySqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                if ((string)dr["status"] == "Processed")
                {
                    EventInfo eventInfo = new EventInfo();
                    eventInfo.idEvent = (int)dr["idEvent"];
                    eventInfo.status = (string)dr["status"];
                    eventInfo.comments = (string)dr["comments"];
                    //if ((int)dr["idEMT"] != null) { eventInfo.idEMT = (int)dr["idEMT"]; }

                    Patient aPatient = new Patient();
                    PatientDAO patients = new PatientDAO();
                    aPatient = patients.getPatientByID((int)dr["idPatient"]);
                    eventInfo.patient = aPatient.name + " " + aPatient.lastName;

                    Triage aTriage = new Triage(); 
                    TriageDAO triage = new TriageDAO();
                    eventInfo.colorCode = triage.getTriageByID((int)dr["idTriage"]).colorCode;

                    events.Add(eventInfo);
                }
            }

            cm.Connection.Close();
            return events; 
        }

        public List<EventInfo> getProcessedEventsInfo()
        {
            List<EventInfo> events = new List<EventInfo>();
            MySqlCommand cm = DBGetCommand(string.Format("SELECT * FROM events"));
            MySqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                if ((string)dr["status"] == "Team on the way")
                {
                    EventInfo eventInfo = new EventInfo();
                    eventInfo.idEvent = (int)dr["idEvent"];
                    eventInfo.status = (string)dr["status"];
                    eventInfo.comments = (string)dr["comments"];
                    //if ((int)dr["idEMT"] != null) { eventInfo.idEMT = (int)dr["idEMT"]; }

                    Patient aPatient = new Patient();
                    PatientDAO patients = new PatientDAO();
                    aPatient = patients.getPatientByID((int)dr["idPatient"]);
                    eventInfo.patient = aPatient.name + " " + aPatient.lastName;

                    Triage aTriage = new Triage();
                    TriageDAO triage = new TriageDAO();
                    eventInfo.colorCode = triage.getTriageByID((int)dr["idTriage"]).colorCode;

                    events.Add(eventInfo);
                }
            }

            cm.Connection.Close();
            return events; 
        }

        public List<EventInfo> getFinishedEventsInfo()
        {
            List<EventInfo> events = new List<EventInfo>();
            MySqlCommand cm = DBGetCommand(string.Format("SELECT * FROM events"));
            MySqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                if ((string)dr["status"] == "Finished")
                {
                    EventInfo eventInfo = new EventInfo();
                    eventInfo.idEvent = (int)dr["idEvent"];
                    eventInfo.status = (string)dr["status"];
                    eventInfo.comments = (string)dr["comments"];
                    //if (dr["idEMT"]) { eventInfo.idEMT = (int)dr["idEMT"]; }

                    Patient aPatient = new Patient();
                    PatientDAO patients = new PatientDAO();
                    aPatient = patients.getPatientByID((int)dr["idPatient"]);
                    eventInfo.patient = aPatient.name + " " + aPatient.lastName;

                    Triage aTriage = new Triage();
                    TriageDAO triage = new TriageDAO();
                    eventInfo.colorCode = triage.getTriageByID((int)dr["idTriage"]).colorCode;

                    events.Add(eventInfo);
                }
            }
            cm.Connection.Close();
            return events; 
        }

        public void createEvent(Event anEvent)
        {
            string query = string.Format("INSERT INTO events (idPatient, idTriage, status, comments)" + "VALUES ('{0}', '{1}', '{2}', '{3}')", anEvent.patient.idPatient, anEvent.triage.idTriage, anEvent.status, anEvent.comments);
            MySqlCommand cm = DBGetCommand(query);
            cm.ExecuteNonQuery();
        }

        public void updateEventStatus(int anID, string aStatus)
        {
            string query = string.Format("UPDATE events SET status = '{0}' WHERE idEvent = '{1}'", aStatus, anID);
            MySqlCommand cm = DBGetCommand(query);
            cm.ExecuteNonQuery();
        }

        public void assignEMTtoEvent(int anID, EMT anEMT)
        {
            string query = string.Format("UPDATE events SET idEMT = '{0}' WHERE idEvent = '{1}'", anEMT.idEMT, anID);
            MySqlCommand cm = DBGetCommand(query);
            cm.ExecuteNonQuery();            
        }

        public Boolean eventsAssignedToThisPatient(int aPatientID)
        {
            PatientDAO patients = new PatientDAO();
            Patient aPatient = patients.getPatientByID(aPatientID);

            List<Event> events = new List<Event>();
            MySqlCommand cm = DBGetCommand(string.Format("SELECT * FROM events WHERE idPatient = '{0}'", aPatient.idPatient));
            MySqlDataReader dr = cm.ExecuteReader();

            if (dr.Read()) 
            {
                return true;
            }

            else 
            {
                return false; 
            }
        }
    }
}
